# -*- coding: utf-8 -*-
"""
Created on Tue Feb 19 14:55:43 2013

@author: aponteeduarddo@gmail.com
Copyright Eduardo Aponte 2013.
"""

# Itereates over the binders

from mex_binder import binder_py
import subprocess as sbp

mx_open_eyelink_connection = binder_py('mx_open_eyelink_connection')
mx_open_eyelink_connection.mx_c.headers = ['<eyelink.h>','"mex.h"']
mx_open_eyelink_connection.mx_c.code = '''

int isscalar (const mxArray *ia){
    if (mxIsNumeric(ia) || mxIsLogical(ia)){
        const int *sia;
        mwSize dia = mxGetNumberOfDimensions(ia);
        sia = mxGetDimensions(ia);
        if ( dia == 2 ){
            if (sia[0] == 1 && sia[1] == 1){
                return 1;
                } else {
                    return 0;
                    }
            } else {
                return 0;
                }
} else {
        return 0;
        }
}


int returnscalar (const mxArray *ia, int *oi ){
    mxClassID id =  mxGetClassID( ia );
    
    if (mxIsComplex(ia)){
        return 0;
    }
    
   if ( id == mxLOGICAL_CLASS ){
        mxLogical  *td;
        td = (mxLogical  *)mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT8_CLASS ){
        signed char *td = (signed char * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT8_CLASS ){
        unsigned char *td = (unsigned char * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT16_CLASS ){
        unsigned short *td = (unsigned short * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT16_CLASS ){
        short *td = (short * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT32_CLASS ){
        unsigned int *td = (unsigned int * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT32_CLASS ){
        int *td = (int * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT64_CLASS ){
        long long *td = (long long * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT64_CLASS ){
        unsigned long long * td = (unsigned long long * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxSINGLE_CLASS ){
        float * td = (float * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxDOUBLE_CLASS ){
            *oi = (int ) *mxGetPr(ia);
            return 1;
    }
    
    return 0;
}


int _main( INT16 iv, INT16 *ov)
{
    if ( iv < -1 || iv > 1 ) return 0;
    *ov = open_eyelink_connection( iv );
    return 1;
}


void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    mwSize td[2] = {1,1};
    double *status;
    /* Verify input arguments. */
    if(nrhs != 1 ) {
        mexErrMsgTxt("Incorrect number of inputs");
    } else if(nlhs>1) {
        mexErrMsgTxt("Too many output arguments.");
    }
    
    if (isscalar(*prhs)){
        INT16 scalar;
        int iv;
        INT16 ov;
        /* Returns the scalar, casts to int if necessary. Returns 0 if */
        /* input is complex. */
        if (returnscalar(*prhs,&iv) != 1 ){
            mexErrMsgTxt("Complex data is not supported.");                        
        }

        if (_main((INT16 ) iv, &ov)){
            plhs[0] = mxCreateNumericArray( 2, td, mxDOUBLE_CLASS, mxREAL);
            status = mxGetPr(plhs[0]);
            *status = (double ) ov;
        } else {
            mexErrMsgTxt("code is not recognized.");   
        }
        
    } else {
         mexErrMsgTxt("code should be scalar.");
    }
    return;
}

'''

mx_open_eyelink_connection.mx_c.mdoc = \
'''function [ status ] = mx_open_eyelink_connection( mode )
%MX_OPEN_EYELINK_CONNECTION Opens a connection with the eyelink.
%   Creates a connection with the eyelink used a defined ip or a default. 
%   Returns 0 is success, otherwise it failed.
%
%   MODE    Either -1,0,1. 0, opens a connection with the eye tracker, 1, 
%           will create a dummy connection for simulation; -1, initializes the library 
%           but does not open a connection.

''' 

mx_open_eyelink_connection.write_c()
mx_open_eyelink_connection.write_m()
mx_open_eyelink_connection.compile()
t = mx_open_eyelink_connection.write_sh()

